#include "widgets.h"
#include "ui_widgets.h"

Widgets::Widgets(QWidget *parent):QWidget(parent),ui(new Ui::Widgets)
{
    ui->setupUi(this);
//    QResource::registerResource(GAME_RCC);

    startgame=new StartWindow;
    connect(startgame,&StartWindow::buttonstartSignal,[=](){
            startgame->close();
            initScene();
            playGame();
            this->show();
    });
}

Widgets::~Widgets()
{
    delete ui;
    delete startgame;
}

void Widgets::initScene()
{
      setFixedSize(GAME_WIDTH,GAME_HEIGHT);
      setWindowTitle(GAME_TITLE);
      setWindowIcon(QIcon(GAME_ICON));

      timer=new QTimer(this);
      timer->setInterval(GAME_RATE);

//      timer2.setInterval(5100);

      enemy_recorder=0;

      soundEffect.setSource(QUrl::fromLocalFile(SOUND_BOMB));
      soundEffect.setVolume(2.0);

      srand((unsigned int)time(NULL));
}
void Widgets::playGame()
{
//    QSound::play(SOUND_BACKGROUND);
    timer->start();
//    timer2.start();

//    connect(&timer2,&QTimer::timeout,[=](){
//       QSound::play(SOUND_BACKGROUND);
//    });
    connect(timer,&QTimer::timeout,[=](){
        enemyToScene();

        updatePosition();

        update();

        collisionDelection();
    });
}

void Widgets::updatePosition()
{
    Map.mapPosition();

    setPosition();

    Plane.shoot();
    for(int i=0;i<BULLET_NUM;i++){
        if(Plane.Bullet[i].bullet_free==false){
            Plane.Bullet[i].bulletPosition();
        }
    }

    for(int i=0;i<ENEMY_NUM;i++){
        if(Enemyplane[i].enemy_free==false){
            Enemyplane[i].updataPosition();
            Enemyplane[i].enemyshort();
        }
    }

    for(int i=0;i<ENEMY_NUM;i++){
        for(int j=0;j<ENEMY_BULLET_NUM;j++){
            if(Enemyplane[i].Enemy_bullet[j].enemy_bullet_free==false){
               Enemyplane[i].Enemy_bullet[j].updatePosition();
            }
        }
    }

    for(int i=0;i<BOMB_NUM;i++){
        if(Bomb[i].bomb_free==false){
            Bomb[i].updateInfo();
        }
    }  

}

void Widgets::paintEvent(QPaintEvent *)
{
    QPainter  painter(this);
    painter.drawPixmap(0,Map.map1_posY,Map.map1);
    painter.drawPixmap(0,Map.map2_posY,Map.map2);
    painter.drawPixmap(Plane.plane_x,Plane.plane_y,Plane.plane_pixmap);
    for(int i=0;i<BULLET_NUM;i++){
        if(Plane.Bullet[i].bullet_free==false){
            painter.drawPixmap(Plane.Bullet[i].bullet_x,Plane.Bullet[i].bullet_y,Plane.Bullet[i].bullet_pixmap);
        }
    }

    for(int i=0;i<ENEMY_NUM;i++){
        for(int j=0;j<ENEMY_BULLET_NUM;j++){
            if(Enemyplane[i].Enemy_bullet[j].enemy_bullet_free==false){
                painter.drawPixmap(Enemyplane[i].Enemy_bullet[j].enemy_bullet_x,Enemyplane[i].Enemy_bullet[j].enemy_bullet_y,Enemyplane[i].Enemy_bullet[j].enemy_bullet_pixmap);
            }
        }
    }

    for(int i=0;i<ENEMY_NUM;i++){
        if(Enemyplane[i].enemy_free==false){
            painter.drawPixmap(Enemyplane[i].enemy_x,Enemyplane[i].enemy_y,Enemyplane[i].enemy_plane);
        }
    }



    for(int i=0;i<BOMB_NUM;i++){
        if(Bomb[i].bomb_free==false){
            painter.drawPixmap(Bomb[i].bomb_x,Bomb[i].bomb_y,Bomb[i].bomb_pixmap);
        }
    }
}

void Widgets::mouseMoveEvent(QMouseEvent *event)
{
    int x=event->x()-PLANE_WIDTH*0.5;
    int y=event->y()-PLANE_HEIGHT*0.5;
    if(x<=0){
        x=0;
    }
    if(x>=GAME_WIDTH-PLANE_WIDTH){
        x=GAME_WIDTH-PLANE_WIDTH;
    }
    if(y<=0){
        y=0;
    }
    if(y>=GAME_HEIGHT-PLANE_HEIGHT){
        y=GAME_HEIGHT-PLANE_HEIGHT;
    }

    Plane.setplanePosition(x,y);
}

void Widgets::keyPressEvent(QKeyEvent *event)
{
    switch(event->key()){
        case Qt::Key_W:
            plane_up=true;
            return;
        case Qt::Key_Up:
            plane_up=true;
            return;
        case Qt::Key_S:
            plane_down=true;
            return;
        case Qt::Key_Down:
            plane_down=true;
            return;
        case Qt::Key_A:
            plane_left=true;
            return;
        case Qt::Key_Left:
            plane_left=true;
            return;
        case Qt::Key_D:
            plane_right=true;
            return;
        case Qt::Key_Right:
            plane_right=true;
            return;
    }
}

void Widgets::keyReleaseEvent(QKeyEvent *event)
{
    switch(event->key()){
        case Qt::Key_W:
            plane_up=false;
            return;
        case Qt::Key_Up:
            plane_up=false;
            return;
        case Qt::Key_S:
            plane_down=false;
            return;
        case Qt::Key_Down:
            plane_down=false;
            return;
        case Qt::Key_A:
            plane_left=false;
            return;
        case Qt::Key_Left:
            plane_left=false;
            return;
        case Qt::Key_D:
            plane_right=false;
            return;
        case Qt::Key_Right:
            plane_right=false;
            return;
    }
}

void Widgets::setPosition()
{
    int x=Plane.plane_x+((plane_left)?-5:0)+((plane_right)?5:0);
    int y=Plane.plane_y+((plane_up)?-5:0)+((plane_down)?5:0);
    if(x<=0){
        x=0;
    }
    if(x>=GAME_WIDTH-PLANE_WIDTH){
        x=GAME_WIDTH-PLANE_WIDTH;
    }
    if(y<=0){
        y=0;
    }
    if(y>=GAME_HEIGHT-PLANE_HEIGHT){
        y=GAME_HEIGHT-PLANE_HEIGHT;
    }

    Plane.setplanePosition(x,y);
}

void Widgets::enemyToScene()
{
    enemy_recorder++;
    if(enemy_recorder<ENEMY_INETRVAL){
        return;
    }

    enemy_recorder=0;
    for(int i=0;i<ENEMY_NUM;i++){
        if(Enemyplane[i].enemy_free){
            Enemyplane[i].enemy_free=false;
            Enemyplane[i].enemy_x=rand()%(GAME_WIDTH-Enemyplane[i].enemy_rect.width());
            Enemyplane[i].enemy_y=-Enemyplane[i].enemy_rect.height();
            break;
        }
    }
}

void Widgets::collisionDelection()
{
    for(int i=0;i<ENEMY_NUM;i++){
        if(Enemyplane[i].enemy_free){
            continue;
        }

        for(int j=0;j<BULLET_NUM;j++){
            if(Plane.Bullet[j].bullet_free){
                continue;
            }
            if(Enemyplane[i].enemy_rect.intersects(Plane.Bullet[j].bullet_rect)){
                soundEffect.play();
                Enemyplane[i].enemy_free=true;
                Plane.Bullet[j].bullet_free=true;

                for(int k=0;k<BOMB_NUM;k++){
                    if(Bomb[k].bomb_free){
                        Bomb[k].bomb_free=false;
                        Bomb[k].bomb_x=Enemyplane[i].enemy_x;
                        Bomb[k].bomb_y=Enemyplane[i].enemy_y;
                        break;
                    }
                }
            }
        }

        if(Enemyplane[i].enemy_rect.intersects(Plane.plane_rect)){
            Enemyplane[i].enemy_free=true;
            for(int k=0;k<BOMB_NUM;k++){
                if(Bomb[k].bomb_free){
                    Bomb[k].bomb_free=false;
                    Bomb[k].bomb_x=Plane.plane_x-20;
                    Bomb[k].bomb_y=Plane.plane_y-20;
                    break;
                }
            }
            for(int k=0;k<BOMB_NUM;k++){
                if(Bomb[k].bomb_free){
                    Bomb[k].bomb_free=false;
                    Bomb[k].bomb_x=Enemyplane[i].enemy_x;
                    Bomb[k].bomb_y=Enemyplane[i].enemy_y;
                    break;
                }
            }
            gameOver();
        }
    }

    for(int i=0;i<ENEMY_NUM;i++){
        for(int j=0;j<ENEMY_BULLET_NUM;j++){
            if(Enemyplane[i].Enemy_bullet[j].enemy_bullet_free){
                continue;
            }
            if(Enemyplane[i].Enemy_bullet[j].enemy_bullet_rect.intersects(Plane.plane_rect)){
                Enemyplane[i].enemy_free=true;
                for(int k=0;k<BOMB_NUM;k++){
                    if(Bomb[k].bomb_free){
                        Bomb[k].bomb_free=false;
                        Bomb[k].bomb_x=Plane.plane_x-20;
                        Bomb[k].bomb_y=Plane.plane_y-20;
                        break;
                    }
                }
                for(int k=0;k<BOMB_NUM;k++){
                    if(Bomb[k].bomb_free){
                        Bomb[k].bomb_free=false;
                        Bomb[k].bomb_x=Enemyplane[i].Enemy_bullet[j].enemy_bullet_x;
                        Bomb[k].bomb_y=Enemyplane[i].Enemy_bullet[j].enemy_bullet_y;
                        break;
                    }
                }
                gameOver();
            }
        }
    }
}

void Widgets::gameOver()
{
    dialog=new QDialog(this);
    dialog->setWindowTitle("游戏结束");
    dialog->resize(200,200);

    vlayout=new QVBoxLayout(dialog);
    button=new QPushButton("重新开始");
    button2=new QPushButton("退出游戏");
    vlayout->addWidget(button);
    vlayout->addWidget(button2);

    connect(button,&QPushButton::clicked,this,&Widgets::buttonSlot);
    connect(button2,&QPushButton::clicked,[=](){
        this->close();
    });

    dialog->exec();
}


void Widgets::buttonSlot()
{
    Plane.setplanePosition((GAME_WIDTH-PLANE_WIDTH)*0.5,GAME_HEIGHT-PLANE_HEIGHT);

    for(int i=0;i<ENEMY_NUM;i++){
        if(Enemyplane[i].enemy_free==false){
            Enemyplane[i].enemy_free=true;
        }
    }

    for(int i=0;i<ENEMY_NUM;i++){
        for(int j=0;j<ENEMY_BULLET_NUM;j++){
            if(Enemyplane[i].Enemy_bullet[j].enemy_bullet_free==false){
               Enemyplane[i].Enemy_bullet[j].enemy_bullet_free=true;
            }
        }
    }

    for(int i=0;i<BULLET_NUM;i++){
        if(Plane.Bullet[i].bullet_free==false){
            Plane.Bullet[i].bullet_free=true;
        }
    }

    for(int k=0;k<BOMB_NUM;k++){
        if(Bomb[k].bomb_free==false){
            Bomb[k].bomb_free=true;
        }
    }

    plane_up=false;
    plane_down=false;
    plane_left=false;
    plane_right=false;

    dialog->close();
}



